package com.foreveredu.propms.web.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Account;
import com.foreveredu.propms.domain.Building;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.Owner;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.service.IBusinessService;
import com.foreveredu.propms.service.IManagementService;

public class DropChangeAJAXAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;
	private IBusinessService businessService = null;
	
	//获得日志记录器
	Logger log = Logger.getLogger(HistoryDebtInputAction.class);
	
	
	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public IBusinessService getBusinessService() {
		return businessService;
	}

	public void setBusinessService(IBusinessService businessService) {
		this.businessService = businessService;
	}

	// 根据物业服务处ID查询DropChangeAJAXAction，获得该物业服务处的小区列表
	public ActionForward changeDropOfSub(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String distId = request.getParameter("distId");
		District dist = managementService.findDistrictById(Integer.parseInt(distId));
		
		List<SubDistrict> subList = new ArrayList<SubDistrict>();
		subList.addAll(dist.getSubs());
		if (subList == null) {
			return null;
		}
		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].subId.length = "
				+ (subList.size() + 1) + ";\n");
		sb.append("document.forms[0].subId.options[0].value = '0';\n");
		sb.append("document.forms[0].subId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].subId.selectedIndex = 0;\n");

		for (int i = 0; i < subList.size(); i++) {
			SubDistrict sub = subList.get(i);
			sb.append("document.forms[0].subId.options[" + (i + 1)
					+ "].value = '" + sub.getId() + "';\n");
			sb.append("document.forms[0].subId.options[" + (i + 1)
					+ "].text = '" + sub.getName() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}
	// 通过下拉列表中的小区ID，获得该小区下的所有楼栋
	public ActionForward changeDropOfBuilding(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String subId = request.getParameter("subId");
		SubDistrict sub = managementService.findSubDistrictById(Integer.parseInt(subId));
		
		List<Building> buildingList = new ArrayList<Building>();
		buildingList.addAll(sub.getBuildings());
		if (buildingList == null) {
			return null;
		}
		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].buildingId.length = "
				+ (buildingList.size() + 1) + ";\n");
		sb.append("document.forms[0].buildingId.options[0].value = '0';\n");
		sb.append("document.forms[0].buildingId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].buildingId.selectedIndex = 0;\n");

		for (int i = 0; i < buildingList.size(); i++) {
			Building b = buildingList.get(i);
			sb.append("document.forms[0].buildingId.options[" + (i + 1)
					+ "].value = '" + b.getId() + "';\n");
			sb.append("document.forms[0].buildingId.options[" + (i + 1)
					+ "].text = '" + b.getName() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}

	// 根据楼栋ID查询DropChangeAJAXAction，获得该楼栋的房产列表
	public ActionForward changeDropOfHouse(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String buildingId = request.getParameter("buildingId");
		Building building = managementService.findBuildingById(Integer.parseInt(buildingId));
		
		List<House> houseList = new ArrayList<House>();
		houseList.addAll(building.getHouses());
		if (houseList == null) {
			return null;
		}
		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].houseId.length = "
				+ (houseList.size() + 1) + ";\n");
		sb.append("document.forms[0].houseId.options[0].value = '0';\n");
		sb.append("document.forms[0].houseId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].houseId.selectedIndex = 0;\n");

		for (int i = 0; i < houseList.size(); i++) {
			House h = houseList.get(i);
			sb.append("document.forms[0].houseId.options[" + (i + 1)
					+ "].value = '" + h.getId() + "';\n");
			sb.append("document.forms[0].houseId.options[" + (i + 1)
					+ "].text = '" + h.getHouseId() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}
	
	// 根据房产编号查询DropChangeAJAXAction，判断该房产是否存在
	public ActionForward findHouseByHouseId(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String houseId = request.getParameter("houseId");
		log.info("houseId="+houseId);
		
		House house = managementService.findHouseByHouseId(houseId);
		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		if(house == null) {
			sb.append("房产编号输入有误，没有找到该房产！");
		} else {
			Owner owner = house.getOwner();
			sb.append("该房产的业主是"+ owner.getName() + "！");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}
	
	//根据业主ID查询DropChangeAJAXAction，获得该业主账户余额
	public ActionForward getBalance(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String ownerId = request.getParameter("ownerId");
		log.info("ownerId="+ownerId);
		
		Owner owner = managementService.findOwnerById(Integer.parseInt(ownerId));
		Account account = businessService.findAccountByOwner(owner);
		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		if(account == null) {
			sb.append("该业主没有开通账户！");
		} else {
			sb.append("该业主的账户余额是"+ account.getBalance() + "元！");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}
}